package com.example.com.transcribeeditpublish.popuWindow;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.example.com.transcribeeditpublish.R;
import com.example.com.transcribeeditpublish.activity.VideoEditActivity;
import com.example.com.transcribeeditpublish.myInterface.IOnBeautyParamsChangeListener;
import com.example.com.transcribeeditpublish.utils.beauty.BeautyParams;
import com.example.com.transcribeeditpublish.views.BeautySettingPannel;

import java.util.ArrayList;

import me.goldze.mvvmhabit.utils.GlidRadioTranform;

public class BeautyPpwindow extends PopupWindow {

    private final View mMenuView;
    private final VideoEditActivity mContext;
    private final IOnBeautyParamsChangeListener listener;
    private ArrayList<Integer> mFilterList;
    private int mCurrentSelectedPos;
    public static final int BEAUTYPARAM_FILTER = 5;
    private String[] mBeautyFilterTypeString = {
            "原画",
            "标准",    // 4
            "樱红",    // 8
            "云裳",    // 8
            "纯真",    // 8
            "白兰",    // 10
            "元气",    // 8
            "超脱",    // 10
            "香氛",    // 5
            "美白",    // 5
            "浪漫",    // 5
            "清新",    // 5
            "唯美",    // 5
            "粉嫩",    // 5
            "怀旧",    // 5
            "蓝调",    // 5
            "清凉",    // 5
            "日系",    // 5
    };

    public BeautyPpwindow(VideoEditActivity videoEditActivity, IOnBeautyParamsChangeListener listener) {
        this.mContext = videoEditActivity;
        this.listener = listener;
        mMenuView = LayoutInflater.from(videoEditActivity).inflate(R.layout.popuwindow_beauty, null, false);
        initRV();
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //适配虚拟键
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000); //android:background="#30000000"
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }

    private void initRV() {
        RecyclerView recycleView = mMenuView.findViewById(R.id.beauty_rv);
        recycleView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        mFilterList = new ArrayList<Integer>();
        mFilterList.add(R.mipmap.orginal);
        mFilterList.add(R.mipmap.biaozhun);
        mFilterList.add(R.mipmap.yinghong);
        mFilterList.add(R.mipmap.yunshang);
        mFilterList.add(R.mipmap.chunzhen);
        mFilterList.add(R.mipmap.bailan);
        mFilterList.add(R.mipmap.yuanqi);
        mFilterList.add(R.mipmap.chaotuo);
        mFilterList.add(R.mipmap.xiangfen);
        mFilterList.add(R.mipmap.langman);
        mFilterList.add(R.mipmap.qingxin);
        mFilterList.add(R.mipmap.weimei);
        mFilterList.add(R.mipmap.fennen);
        mFilterList.add(R.mipmap.huaijiu);
        mFilterList.add(R.mipmap.landiao);
        mFilterList.add(R.mipmap.qingliang);
        mFilterList.add(R.mipmap.rixi);
        ((SimpleItemAnimator) recycleView.getItemAnimator()).setSupportsChangeAnimations(false);
        recycleView.setAdapter(new BeauutyAdapter());


    }

    public void show(VideoEditActivity videoEditActivity) {
        this.showAtLocation(videoEditActivity.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }

    public String[] getBeautyFilterArr() {
        return mBeautyFilterTypeString;
    }
    private class BeauutyAdapter extends RecyclerView.Adapter<BHolder> {
        @Override
        public BHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.filter_layout, parent, false);
            return new BHolder(view);
        }

        @Override
        public void onBindViewHolder(BHolder holder, final int position) {

            Glide.with(mContext).load(mFilterList.get(position)).transform(new GlidRadioTranform(mContext)).into(holder.ivImage);
            Glide.with(mContext).load(R.mipmap.filter_selected).transform(new GlidRadioTranform(mContext)).into(holder.ivImageTint);

            if (mCurrentSelectedPos == position) {
                holder.ivImageTint.setVisibility(View.VISIBLE);
            } else {
                holder.ivImageTint.setVisibility(View.GONE);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setCurrentSelectedPos(position);
                }
            });
        }

        public void setCurrentSelectedPos(int pos) {
            int tPos = mCurrentSelectedPos;
            mCurrentSelectedPos = pos;
            this.notifyItemChanged(tPos);
            this.notifyItemChanged(mCurrentSelectedPos);
            setFilter(mCurrentSelectedPos);
        }

        @Override
        public int getItemCount() {
            return mFilterList.size();
        }
    }

    private class BHolder extends RecyclerView.ViewHolder {
        public final ImageView ivImage;
        public final ImageView ivImageTint;

        public BHolder(View view) {
            super(view);
            ivImage = (ImageView) itemView.findViewById(R.id.filter_image);
            ivImageTint = (ImageView) itemView.findViewById(R.id.filter_image_tint);
        }
    }

    //设置滤镜
    private void setFilter(int index) {
        Bitmap bmp = getFilterBitmapByIndex(index);
        if (listener != null) {
            BeautyParams params = new BeautyParams();
            params.mFilterBmp = bmp;
            params.filterIndex = index;
            listener.onBeautyParamsChange(params, BEAUTYPARAM_FILTER);
        }
    }

    public Bitmap getFilterBitmapByIndex(int index) {
        Bitmap bmp = null;
        switch (index) {
            case 1:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_biaozhun);
                break;
            case 2:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_yinghong);
                break;
            case 3:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_yunshang);
                break;
            case 4:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_chunzhen);
                break;
            case 5:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_bailan);
                break;
            case 6:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_yuanqi);
                break;
            case 7:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_chaotuo);
                break;
            case 8:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_xiangfen);
                break;
            case 9:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_white);
                break;
            case 10:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_langman);
                break;
            case 11:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_qingxin);
                break;
            case 12:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_weimei);
                break;
            case 13:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_fennen);
                break;
            case 14:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_huaijiu);
                break;
            case 15:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_landiao);
                break;
            case 16:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_qingliang);
                break;
            case 17:
                bmp = decodeResource(mContext.getResources(), R.drawable.filter_rixi);
                break;
            default:
                bmp = null;
                break;
        }
        return bmp;
    }

    private Bitmap decodeResource(Resources resources, int id) {
        TypedValue value = new TypedValue();
        resources.openRawResource(id, value);
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inTargetDensity = value.density;
        return BitmapFactory.decodeResource(resources, id, opts);
    }

}
